<?php

namespace MsgCode;
use think\facade\Env;
use think\facade\Validate;

/**
 * 虚拟手机号码
 */
class FictitiousPhone{
    private static  $weblist = array(
        '1'=>'BirdCode',
        '2'=>'SuperCloud',
        '3'=> 'PrivateSMS'
    );			    //@$weblist	可获取的断行途径
    private static  $notallowed = ['SuperCloud'];           //不允许访问
    private static  $single; 									// 句柄
    private static  $class_name;                                // 类函数名
    private static $classPond;                                  // 以实例化的类池
    private     $class;                                             // 当前类
    private     $dir;                                             // 类位置
    public      $errorMsg;                                             // 错误信息

    /**
     * FictitiousPhone constructor. 初始化
     */
    private function __construct()
    {
        // EXTEND_PATH  获取extend地址
        $this->dir||$this->dir = Env::get('EXTEND_PATH');
        // 操控html类
        require_once $this->dir."MsgCode/lib/simple_html_dom.php";
        require_once $this->dir."MsgCode/way/".self::$class_name.".php";
        $this->class = new self::$class_name();
    }


    /**    单例统一访问入口
     * @param string $web       接码网站
     * @return bool
     */
    static public  function  init($web='1')
    {
        if (!Validate::in($web,['1','2','3'])){return '参数错误,网站code码错误'; }
        if (Validate::in(self::$weblist[$web], self::$notallowed)){return '该站调整中！敬请期待！！！！'; }

        //  实例化对象或则  获取类池中的实例
        if (self::$weblist[$web] != self::$class_name) {
            // 当前类的名称
            self::$class_name = self::$weblist[$web];
            if (empty(self::$classPond[$web])) {
                self::$single = new self();
                self::$classPond[$web] = self::$single;
            }
        }
        self::$single = self::$classPond[$web];
        return self::$single;
    }

    function getPhone(){
        return $this->class->PhoneList;
    }

    /**
     *  私有克隆函数，防止外办克隆对象
     */
    private function __clone() {return "小样！（‘_’）";}

    /** 获取手机列表数据
     * @param $num
     * @param $Tellist
     * @return mixed
     */
    private function getPhoneArray($num, $Tellist){
        $list = array();
        if (!empty($Tellist)){
            foreach ($Tellist as $v)
                $list[] = $v."_".$num;
        }
        return $list;
    }

    /** 获取手机短信
     * @param $phone
     */
    function getMssage($phone){
        $phoneInfo = explode('_',$phone);
        require_once self::$dir."MsgCode/way/".self::$weblist[$phoneInfo[2]].".php";
        $class = new self::$weblist[$phoneInfo[2]];
        $class->getMessage($phoneInfo[1]);
        $this->Msg = $class->Mssage;
    }
}